package com.aos.erp.schools.rest.student;

import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.schools.cutils.Constant;
import com.aos.erp.schools.cutils.TimeUtils;
import com.aos.erp.schools.cutils.router.Rest;
import com.aos.erp.schools.enums.student.StudentStatus;
import com.aos.erp.schools.model.gatherschool.entity.GatherSchool;
import com.aos.erp.schools.model.student.Student;
import com.aos.erp.schools.model.student.StudentExam;
import com.aos.erp.schools.model.student.StudentExamHistory;
import com.aos.erp.schools.model.student.dto.StudentOneDto;
import com.aos.erp.schools.model.student.ro.exam.AddStudentExamRo;
import com.aos.erp.schools.model.student.ro.exam.StudentYueKaoRo;
import com.aos.erp.schools.model.student.ro.exam.UpdateYueKaoRo;
import com.aos.erp.schools.model.student.ro.student.SelectByStateRo;
import com.aos.erp.schools.model.student.vo.eaxm.ShowUpdateExamVo;
import com.aos.erp.schools.model.student.vo.eaxm.StudentYueKaoExamVo;
import com.aos.erp.schools.model.student.vo.student.StudentAllVo;
import com.aos.erp.schools.model.student.vo.student.StudentEaxmVo;
import com.aos.erp.schools.service.student.StudentExamHistoryService;
import com.aos.erp.schools.service.student.StudentExamService;
import com.aos.erp.schools.service.student.StudentService;
import com.aos.erp.schools.util.UserUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @Auther: chb
 * @Date: 2018/11/1 19:22
 * @Description:    学员预约考试
 */
@RestController
@RequestMapping(Rest.StudentExam.ROOT)
@Api(tags = {"学员预约考试信息"})
public class StudentExamController {

    @Autowired
    private StudentExamService studentExamService;

    @Autowired
    private StudentService studentService ;

    @Resource
    private StudentExamHistoryService studentExamHistoryService;

    @GetMapping(Rest.StudentExam.queryByStateK1)
    @ApiOperation(value = "科目一")
    public ResultEntity<PageBean<StudentEaxmVo>> queryByStateK1(SelectByStateRo ro) {
        try {

            if (StringUtils.isEmpty(ro.getCurrentUserId())) {
                return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
            }
            GatherSchool driversSchool= UserUtil.getUserSchool(ro.getCurrentUserId());
            if(driversSchool==null){
                return ResultEntity.failed("驾校数据不存在");
            }
            ro.setDriversId(driversSchool.getId());
            ro.setSubject(1);
            ro.setTimeStartName(TimeUtils.start(ro.getTimeStart()));
            ro.setTimeEddName(TimeUtils.edd(ro.getTimeEdd()));
            PageBean<StudentEaxmVo> student = studentExamService.queryStudentYueKao(ro);
            return ResultEntity.success(student);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed("科目一查询失败!");
        }
    }


    @GetMapping(Rest.StudentExam.queryByStateK2)
    @ApiOperation(value = "科目二管理")
    public ResultEntity<PageBean<StudentEaxmVo>> queryByStateK2(SelectByStateRo ro) {
        try {
            if (StringUtils.isEmpty(ro.getCurrentUserId())) {
                return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
            }
            GatherSchool driversSchool= UserUtil.getUserSchool(ro.getCurrentUserId());
            if(driversSchool==null){
                return ResultEntity.failed("驾校数据不存在");
            }
            ro.setDriversId(driversSchool.getId());
            ro.setSubject(2);
            ro.setTimeStartName(TimeUtils.start(ro.getTimeStart()));
            ro.setTimeEddName(TimeUtils.edd(ro.getTimeEdd()));
            PageBean<StudentEaxmVo> student = studentExamService.queryStudentYueKao(ro);
            return ResultEntity.success(student);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed("科目二查询失败!");
        }
    }


    @GetMapping(Rest.StudentExam.queryByStateK3)
    @ApiOperation(value = "科目三管理")
    public ResultEntity<PageBean<StudentEaxmVo>> queryByStateK3(SelectByStateRo ro) {
        try {
            if (StringUtils.isEmpty(ro.getCurrentUserId())) {
                return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
            }
            GatherSchool driversSchool= UserUtil.getUserSchool(ro.getCurrentUserId());
            if(driversSchool==null){
                return ResultEntity.failed("驾校数据不存在");
            }
            ro.setDriversId(driversSchool.getId());
            ro.setSubject(3);
            ro.setTimeStartName(TimeUtils.start(ro.getTimeStart()));
            ro.setTimeEddName(TimeUtils.edd(ro.getTimeEdd()));
            PageBean<StudentEaxmVo> student = studentExamService.queryStudentYueKao(ro);
            return ResultEntity.success(student);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed("科目三查询失败!");
        }
    }


    @GetMapping(Rest.StudentExam.queryByStateK4)
    @ApiOperation(value = "科目四管理")
    public ResultEntity<PageBean<StudentEaxmVo>> queryByStateK4(SelectByStateRo ro) {
        try {
            if (StringUtils.isEmpty(ro.getCurrentUserId())) {
                return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
            }
            GatherSchool driversSchool= UserUtil.getUserSchool(ro.getCurrentUserId());
            if(driversSchool==null){
                return ResultEntity.failed("驾校数据不存在");
            }
            ro.setDriversId(driversSchool.getId());
            ro.setSubject(4);
            ro.setTimeStartName(TimeUtils.start(ro.getTimeStart()));
            ro.setTimeEddName(TimeUtils.edd(ro.getTimeEdd()));
            PageBean<StudentEaxmVo> student = studentExamService.queryStudentYueKao(ro);
            return ResultEntity.success(student);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed("科目四查询失败!");
        }
    }




    @GetMapping(Rest.StudentExam.queryStudentYueKaoExam)
    @ApiOperation(value = "查询达到约考条件的学员")
    public ResultEntity<PageBean<StudentYueKaoExamVo>> queryStudentYueKaoExam(StudentYueKaoRo ro) {
        try {

            //TODO 约考火候的学员不能再约考了 要取消约考 ，再重新约考
            if (StringUtils.isEmpty(ro.getCurrentUserId())) {
                return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
            }
            GatherSchool driversSchool= UserUtil.getUserSchool(ro.getCurrentUserId());
            if(driversSchool==null){
                return ResultEntity.failed("驾校数据不存在");
            }
            ro.setDriverId(driversSchool.getId());
            //判断 ro.getExamSubject()  是否 有值 TODO
            int state=StudentStatus.newenroll.getValues();
            if(StringUtils.isEmpty(ro.getExamSubject()))
            {
                return ResultEntity.failed("科目不能为空!");
            }
            switch (ro.getExamSubject())
            {
                case Constant.k1:{
                    state=StudentStatus.joink1.getValues();
                    break;
                }
                case Constant.k2:
                    state=StudentStatus.joink2.getValues();
                    break;

                case Constant.k3:{
                    state=StudentStatus.joink3.getValues();
                    break;
                }
                case Constant.k4:{
                    state=StudentStatus.joink4.getValues();
                    break;
                }
            }
            ro.setState(state);
            PageBean<StudentYueKaoExamVo> student = studentExamService.queryStudentYueKaoExam(ro);
            return ResultEntity.success(student);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed("查询失败!");
        }
    }


    @PostMapping("/studentYueKaoExamAdd")
    @ApiOperation(value = "学员预约考试信息添加--自身平台")
    public ResultEntity addStudentExam(@RequestBody  List<AddStudentExamRo> ro,@RequestParam(name = "currentUserId", required = false) String currentUserId){
        try {
            if (StringUtils.isEmpty(currentUserId)) {
                return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
            }
            GatherSchool driversSchool= UserUtil.getUserSchool(currentUserId);
            if(driversSchool==null){
                return ResultEntity.failed("驾校数据不存在");
            }
            List<StudentExamHistory> listHistory=new LinkedList<>();
            for(AddStudentExamRo addro : ro)
            {
                //用学员的id  查询出学员的信息  保存到约考信息表中
                StudentAllVo student=studentService.queryObject(addro.getStudentId());
                if(student==null)
                {
                    return ResultEntity.failed("学员约考学员不存在!");
                }
                StudentExam se=new StudentExam();
                BeanUtils.copyProperties(addro,se);
                se.setDriverId(driversSchool.getId());
                se.setCreateUser(Long.parseLong(currentUserId));
                se.setType(1);//自身平台 TODO
                se.setStudentId(student.getId());
                se.setState(0);//1表示为预约成功
                studentExamService.saveBean(se);
                StudentExamHistory seh=new StudentExamHistory();
                BeanUtils.copyProperties(addro,seh);
                seh.setStudentId(student.getId());
                listHistory.add(seh);
            }
            studentExamHistoryService.saveBatch(listHistory);
            return ResultEntity.success("学员约考成功!");

        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed("学员约考失败,请联系管理员!");
        }
    }


    @PostMapping("/updateYueKaoExam")
    @ApiOperation(value = "取消  预约")
    public ResultEntity updateYueKaoExam(@RequestBody List<UpdateYueKaoRo> ro,@RequestParam(name = "currentUserId", required = false) String currentUserId) throws Exception{
        if (StringUtils.isEmpty(currentUserId)) {
            return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
        }
        GatherSchool driversSchool= UserUtil.getUserSchool(currentUserId);
        if(driversSchool==null){
            return ResultEntity.failed("驾校数据不存在");
        }
        try {
            for(UpdateYueKaoRo yro : ro)
            {
                 yro.setState(-1L);
                studentExamService.updateYueKaoExam(yro);

            }
            return ResultEntity.success("学员约考状态修改成功!");
            }
             catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed("学员约考状态修改失败,请联系管理员!");
        }
    }


    @GetMapping("/showUpdateExam")
    @ApiOperation(value = "学员预约考试编辑 初始化的数据")
    public ResultEntity<ShowUpdateExamVo> showUpdateExam(@RequestParam(name = "currentUserId", required = false) String currentUserId,long studentId){
        try {
            if (StringUtils.isEmpty(currentUserId)) {
                return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
            }
            ShowUpdateExamVo showVo= studentExamService.showUpdateExam(studentId);
            return ResultEntity.success(showVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed("学员约考查询失败,请联系管理员!");
        }
    }


    @PostMapping("/saveUpdateEaxmData")
    @ApiOperation(value = "学员预约考试编辑   保存")
    public ResultEntity saveUpdateEaxmData(@RequestParam(name = "currentUserId", required = false) String currentUserId,StudentExam ro){
        try {
            if (StringUtils.isEmpty(currentUserId)) {
                return ResultEntity.failed("请重新登录 ", ResultEntity.CODE_NULL);
            }

            StudentExam se=studentExamService.selectOne(ro.getId());
            if(se==null)
            {
                return ResultEntity.failed("学员约考修改保存失败");
            }
            if(se.getDegree()==null)
            {
                ro.setDegree(1L);
            }else
                {
                    ro.setDegree(se.getDegree()+1L);//次数加一
                }

            int num= studentExamService.saveUpdateEaxmData(ro);
            if(num>0)
            {
                return ResultEntity.success("学员约考修改保存成功");
            }
            return ResultEntity.failed("学员约考修改保存失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed("学员约考修改保存失败,请联系管理员!");
        }
    }





}
